package com.aiju.cash.ui.main;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.aiju.base.activity.BaseTitleActivity;
import com.aiju.cash.R;
import com.aiju.cash.core.api.DataJson;
import com.aiju.cash.core.api.RefundApi;
import com.aiju.cash.core.event.BlotterEvent;
import com.aiju.cash.ui.main.entity.Refund;
import com.aiju.common.ActivityStack;
import com.aiju.common.event.EventCenter;
import com.aiju.utils.PriceUtil;
import com.aiju.utils.T;
import com.aiju.widgit.dialog.SimpleDialog;
import com.zhy.http.okhttp.callback.StringCallback;

import okhttp3.Call;

/**
 * @author: 佐为
 * @date: 2017-02-16 10:55
 * @version: V1.0
 * @Description: 退款成功
 */
public class RefundSuccessActivity extends BaseTitleActivity {


    private TextView money, state;

    private Button button;

    private ImageView imageView;

    private String price;

    private Refund refund;
    private boolean success;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_refund_success);


        price = getIntent().getStringExtra("price");
        String type = getIntent().getStringExtra("type");
        success = getIntent().getBooleanExtra("success", false);

        if (!success && getIntent().hasExtra("refund")) {
            refund = (Refund) getIntent().getSerializableExtra("refund");
        }

        state = (TextView) findViewById(R.id.tv_state);
        money = (TextView) findViewById(R.id.tv_money);
        button = (Button) findViewById(R.id.btn_refund);
        imageView = (ImageView) findViewById(R.id.iv_refund);

        if (success) {
            setCenterTitle("退款成功");
            state.setText("操作成功");
            money.setText("已成功退款" + PriceUtil.formatDouble(price) + "元");
            button.setText("继续查看店铺流水");
            imageView.setImageResource(R.mipmap.icon_refund_success);
        } else {
            setCenterTitle("退款失败");
            state.setText("操作失败");
            money.setText(type + "退款失败，建议您选择现金退款或稍后重试");
            button.setText("更换退款方式");
            imageView.setImageResource(R.mipmap.icon_refund_error);
        }
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (success) {
                    EventCenter.getInstance().post(new BlotterEvent());
                    ActivityStack.cleanTop(3);
                } else {
                    cashDialog();
                }
            }
        });
    }

    @Override
    public void onBackPressed() {
        if (success) {
            EventCenter.getInstance().post(new BlotterEvent());
            ActivityStack.cleanTop(3);
        } else {
            super.onBackPressed();
        }
    }

    /**
     * 现金退款提示
     */
    private void cashDialog() {
        new SimpleDialog(this)
                .setLayoutParams()
                .setTitileText("请确认您的" + PriceUtil.formatDouble(price) + "元已经以现金方式退还给顾客")
                .setOnSimpleClick(new SimpleDialog.OnSimpleClick() {
                    @Override
                    public void onCancel() {
                    }

                    @Override
                    public void onSubmit() {
                        cashRefund();
                    }
                }).show();
    }

    private void cashRefund() {
        mDialogHelper.showLoadingProgressDialog();
        new RefundApi().cashRefund(mUser.getStoreId(),
                refund.serialNum,
                refund.refundType,
                refund.price,
                refund.refundPrice, mUser.getOperatorId(),
                mUser.getUserName(),
                refund.cause, refund.outRefundNo, new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        mDialogHelper.dismissProgressDialog();
                        T.ToastNetError();
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        mDialogHelper.dismissProgressDialog();
                        DataJson dataJson = new DataJson(response);
                        if (dataJson.isPaySuccess()) {
                            goRefundView(true);
                        } else {
                            goRefundView(false);
                        }
                    }
                }
        );
    }


    /**
     * 跳转成功界面
     */
    private void goRefundView(boolean success) {
        finish();
        Intent intent = new Intent(RefundSuccessActivity.this, RefundSuccessActivity.class);
        intent.putExtra("price", refund.refundPrice);
        intent.putExtra("type", refund.type);
        intent.putExtra("success", success);
        if (!success) {
            intent.putExtra("refund", refund);
        }
        startActivity(intent);
    }
}
